package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.bean.WorkBean;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.mapper.ReceiptTbMapper;
import com.dqyt.khjc.mapper.WorkMapper;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.NumberUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.management.timer.TimerMBean;
import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ReceiptTbService {

    @Autowired
    private ReceiptTbMapper receiptTbMapper;

    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private HistoryMapper historyMapper;

    /**
     * 新增接收单
     * @param request
     * @param rwdbh
     * @param hxqNumber
     * @param fhqpNumber
     * @param startNumber
     * @param endNumber
     * @param wtfId
     * @param wtf
     * @param sjr
     * @param jsrq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg addReceipt(HttpServletRequest request, String rwdbh, int hxqNumber, int fhqpNumber,
                                String startNumber, String endNumber, long wtfId, String wtf, String sjr,String sjrPhone,
                                String jsrq,String bz,long wtfchId,String wtfch,
                                int mzjy , int mzwx,int qpjy,int qpcz,int ghqp,int ghpf){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_YPJSD)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }

            if("".equals(hxqNumber)|| (hxqNumber==0 && fhqpNumber ==0) ){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }

            int maxNumber =0;
            if(hxqNumber>fhqpNumber){
                maxNumber = hxqNumber;
            }else{
                maxNumber = fhqpNumber;
            }
            if(startNumber.length()<5){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("开始号至少为5位");
                return ret;
            }

            //如果只有开始号
            if("".equals(endNumber)){
                if(maxNumber>1){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("复合气瓶或者面罩数量大于1，必须填写结束号");
                    return ret;
                }
            }else{
                if(endNumber.length() !=startNumber.length()){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("开始号和结束好的位数要保持一致");
                    return ret;
                }
                int startNumberInt = Integer.parseInt(startNumber);
                int endNumberInt = Integer.parseInt(endNumber);
                //如果计算的数量 和最大值不等
                if((endNumberInt-startNumberInt+1) !=maxNumber){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("填写的开始编号和结束边和与气瓶数量或者面罩数量不匹配");
                    return ret;
                }
            }

            String nianStr = rwdbh.substring(0,4);
            //在做一次检查  看看有没有 重复的 标识编号在数据库
            //创建呼吸器数组
            String[] hxqArray= new String[hxqNumber];
            for(int j =0;j<hxqNumber;j++){
                int startNumberInt = Integer.parseInt(startNumber);
                int mybsbh =startNumberInt+j;
                String mybsbhStr =String.valueOf(mybsbh);
                int mlength = startNumber.length() -mybsbhStr.length();
                if(wtf.equals("辽河油田录井公司")){
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh)+"LH";;
                    hxqArray[j] = zhBsbh;
                }else{
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh);
                    hxqArray[j] = zhBsbh;
                }

            }
            //添加 符合气瓶
            //创建气瓶数组
            String[] qpArray= new String[fhqpNumber];
            for(int k =0;k<fhqpNumber;k++){
                int startNumberInt = Integer.parseInt(startNumber);
                int mybsbh =startNumberInt+k;
                String mybsbhStr =String.valueOf(mybsbh);
                int mlength = startNumber.length() -mybsbhStr.length();
                if(wtf.equals("辽河油田录井公司")){
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh) +"LH";
                    qpArray[k] = zhBsbh;
                }else{
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh);
                    qpArray[k] = zhBsbh;
                }

            }
            List<WorkBean> workBeansALL =new ArrayList<>();

            if(hxqArray.length != 0){
                List<WorkBean> workBeans33 = workMapper.batchBsbhList(hxqArray);
                workBeansALL.addAll(workBeans33);
            }

            if(qpArray.length != 0){
                List<WorkBean> workBeans44 = workMapper.batchBsbhList(qpArray);
                workBeansALL.addAll(workBeans44);
            }

            List<String > basbhss = new ArrayList<>();
             if(workBeansALL.size()>0){
                String cfBsbh ="";
                for(WorkBean mw:workBeansALL){
                    String zybh= mw.getBsbh();
                    basbhss.add(zybh.substring(4,zybh.length()));
                }
                ret.setCode(88);
                ret.setData(basbhss);
                return ret;
            }

            //查询如果有一个一样的任务单就提示
            ReceiptTbBean receiptTbBean = receiptTbMapper.getReceiptByrwdbh(rwdbh);
            if(null != receiptTbBean){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("此任务单已经存在，不能重复添加");
                return ret;
            }

            ReceiptTbBean receiptTbBean2 = new ReceiptTbBean(rwdbh, hxqNumber, fhqpNumber, startNumber, endNumber,
             wtfId,  wtf, sjr, sjrPhone, user.getUserId(), user.getUserName(), jsrq,bz, 0);
            receiptTbBean2.setAddTime(TimeUtil.currentTimeMillis());
            receiptTbBean2.setAddUser(user.getUserId()+"");
            receiptTbBean2.setUpdateUser("");
            receiptTbBean2.setUpdateTime(0);
            receiptTbBean2.setWtfchId(wtfchId);
            receiptTbBean2.setWtfch(wtfch);

            receiptTbBean2.setMzjy(mzjy);
            receiptTbBean2.setMzwx(mzwx);
            receiptTbBean2.setQpjy(qpjy);
            receiptTbBean2.setQpcz(qpcz);
            receiptTbBean2.setGhqp(ghqp);
            receiptTbBean2.setGhpf(ghpf);

            int res =  receiptTbMapper.addReceipt(receiptTbBean2);
           //添加成功之后
           if(res>0){
               //添加呼吸器
               for(int a =0;a<hxqNumber;a++){
                   int startNumberInt = Integer.parseInt(startNumber);
                   int mybsbh =startNumberInt+a;
                   String mybsbhStr =String.valueOf(mybsbh);
                   int mlength = startNumber.length() -mybsbhStr.length();
                   if(wtf.equals("辽河油田录井公司")){
                       String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh)+"LH";
                       WorkBean workBean = new WorkBean(rwdbh,1, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                       //将接收日期转化为时间戳
                       long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                       workBean.setJsrqLong(jsrqJjc);
                       workBean.setAddUser(user.getUserId()+"");
                       workBean.setAddTime(TimeUtil.currentTimeMillis());

                       workBean.setWtfchId(wtfchId);
                       workBean.setWtfch(wtfch);

                       //添加排序字段
                       String pxzdStr = workBean.getBsbh().replace("DQFJ","");
                       workBean.setPxzd(pxzdStr);
                       workMapper.addWork(workBean);
                   }else{
                       String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh);
                       WorkBean workBean = new WorkBean(rwdbh,1, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                       workBean.setAddUser(user.getUserId()+"");
                       //将接收日期转化为时间戳
                       long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                       workBean.setJsrqLong(jsrqJjc);
                       workBean.setAddTime(TimeUtil.currentTimeMillis());
                       workBean.setWtfchId(wtfchId);
                       workBean.setWtfch(wtfch);

                       //添加排序字段
                       String pxzdStr = workBean.getBsbh().replace("DQFJ","");
                       workBean.setPxzd(pxzdStr);


                       workMapper.addWork(workBean);
                   }

               }
               //添加 符合气瓶
               for(int c =0;c<fhqpNumber;c++){
                   int startNumberInt = Integer.parseInt(startNumber);
                   int mybsbh =startNumberInt+c;
                   String mybsbhStr =String.valueOf(mybsbh);
                   int mlength = startNumber.length() -mybsbhStr.length();
                   if(wtf.equals("辽河油田录井公司")){
                       String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh)+"LH";;
                       WorkBean workBean = new WorkBean(rwdbh,2, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                       workBean.setAddUser(user.getUserId()+"");
                       //将接收日期转化为时间戳
                       long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                       workBean.setJsrqLong(jsrqJjc);
                       workBean.setAddTime(TimeUtil.currentTimeMillis());
                       workBean.setWtfchId(wtfchId);
                       workBean.setWtfch(wtfch);

                       //添加排序字段
                       String pxzdStr = workBean.getBsbh().replace("YTQP","");
                       workBean.setPxzd(pxzdStr);

                       workMapper.addWork(workBean);
                   }else{
                       String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh);
                       WorkBean workBean = new WorkBean(rwdbh,2, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                       workBean.setAddUser(user.getUserId()+"");
                       //将接收日期转化为时间戳
                       long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                       workBean.setJsrqLong(jsrqJjc);
                       workBean.setAddTime(TimeUtil.currentTimeMillis());
                       workBean.setWtfchId(wtfchId);
                       workBean.setWtfch(wtfch);
                       //添加排序字段
                       String pxzdStr = workBean.getBsbh().replace("YTQP","");
                       workBean.setPxzd(pxzdStr);
                       workMapper.addWork(workBean);
                   }
               }

               ret.setCode(ResultMsg.SUCESS);
               ret.setMsg("添加成功");


               //如果操作日志的开关已经打开
               if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                   //添加操作记录
                   // 先取得用户ip
                   String ip = IpUtil.getIpAddr(request);
                   //被操作者名字
                   String namesStr = JSON.toJSONString(rwdbh);
                   //被操作者id
                   String bopIdStr = JSON.toJSONString(rwdbh);
                   HistoryBean historyBean = new HistoryBean();
                   historyBean.setMessage("[" + user.getUserName() + "] 新增了任务单编号为 " + rwdbh + " 的样品接收单");
                   historyBean.setType(Opt.YPGL_YPJSD_XZJSD);
                   historyBean.setOpUserId(user.getUserId());
                   historyBean.setIp(ip);
                   historyBean.setBopId(bopIdStr);
                   historyBean.setAddTime(TimeUtil.currentTimeMillis());
                   historyBean.setAddUser(String.valueOf(user.getUserId()));
                   historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                   historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                   //插入数据库
                   historyMapper.addHistory(historyBean);
               }


           }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 新增接收单 手动输入编号
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg addReceipt3(HttpServletRequest request,ReceiptTbBeanDTO receiptTbBeanDTO){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_YPJSD)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }

            if((receiptTbBeanDTO.getHxqNumber())==null|| (receiptTbBeanDTO.getHxqNumber()==0 && receiptTbBeanDTO.getFhqpNumber() ==0) ){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            //查询如果有一个一样的任务单就提示
            ReceiptTbBean receiptTbBean = receiptTbMapper.getReceiptByrwdbh(receiptTbBeanDTO.getRwdbh());
            if(null != receiptTbBean){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("此任务单已经存在，不能重复添加");
                return ret;
            }


            //在做一次检查  看看有没有 重复的 标识编号在数据库
            //创建呼吸器数组

            String[] hxqArray= receiptTbBeanDTO.getMzList();
            for(int j =0;j<hxqArray.length-1;j++){
                if(receiptTbBeanDTO.getWtf().equals("辽河油田录井公司")){
                    hxqArray[j] += "LH";
                }
            }
            //添加 符合气瓶
            //创建气瓶数组
            String[] qpArray= receiptTbBeanDTO.getQpList();
            for(int k =0;k<qpArray.length-1;k++){
                if(receiptTbBeanDTO.getWtf().equals("辽河油田录井公司")){
                    qpArray[k]+= "LH";
                }
            }
            List<WorkBean> workBeansALL =new ArrayList<>();

            if(hxqArray[0] != ""){
                List<WorkBean> workBeans33 = workMapper.batchBsbhList(hxqArray);
                workBeansALL.addAll(workBeans33);
            }

            if(qpArray[0] != ""){
                List<WorkBean> workBeans44 = workMapper.batchBsbhList(qpArray);
                workBeansALL.addAll(workBeans44);
            }

            List<String > basbhss = new ArrayList<>();
            if(workBeansALL.size()>0){
                for(WorkBean mw:workBeansALL){
                    String zybh= mw.getBsbh();
                    if(!Objects.equals(zybh, "")){
                        basbhss.add(zybh.substring(4));
                    }

                }
                ret.setCode(88);
                ret.setData(basbhss);
                return ret;
            }
            ReceiptTbBean receiptTbBean2 = new ReceiptTbBean();
            BeanUtils.copyProperties(receiptTbBeanDTO,receiptTbBean2);

            receiptTbBean2.setJsr(user.getUserName());
            receiptTbBean2.setAddTime(TimeUtil.currentTimeMillis());
            receiptTbBean2.setAddUser(user.getUserId()+"");


            int res =  receiptTbMapper.addReceipt(receiptTbBean2);
            //添加成功之后
            if(res>0){
                //添加呼吸器
                String[] mzList = receiptTbBeanDTO.getMzList();
                for(int a =0;a<=mzList.length-1;a++){
//
                    String bsbh = "";
                    if(receiptTbBeanDTO.getWtf().equals("辽河油田录井公司")){
                        bsbh=mzList[a]+"LH";
                        WorkBean workBean = new WorkBean(receiptTbBeanDTO.getRwdbh(),1, bsbh, receiptTbBeanDTO.getWtfId(),
                                receiptTbBeanDTO.getWtf(), receiptTbBeanDTO.getSjr(), user.getUserId(),  user.getUserName(), receiptTbBeanDTO.getJsrq(), 1);
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5( receiptTbBeanDTO.getJsrq());
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddUser(user.getUserId()+"");
                        workBean.setAddTime(TimeUtil.currentTimeMillis());

                        workBean.setWtfchId(receiptTbBeanDTO.getWtfchId());
                        workBean.setWtfch(receiptTbBeanDTO.getWtfch());

                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("DQFJ","");
                        workBean.setPxzd(pxzdStr);
                        workMapper.addWork(workBean);
                    }else{

                        WorkBean workBean = new WorkBean(receiptTbBeanDTO.getRwdbh(),1, mzList[a], receiptTbBeanDTO.getWtfId(), receiptTbBeanDTO.getWtf(),
                                receiptTbBeanDTO.getSjr(), user.getUserId(),  user.getUserName(), receiptTbBeanDTO.getJsrq(), 1);
                        workBean.setAddUser(user.getUserId()+"");
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(receiptTbBeanDTO.getJsrq());
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddTime(TimeUtil.currentTimeMillis());
                        workBean.setWtfchId(receiptTbBeanDTO.getWtfchId());
                        workBean.setWtfch(receiptTbBeanDTO.getWtfch());

                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replaceAll("DQFJ","");
                        workBean.setPxzd(pxzdStr);


                        workMapper.addWork(workBean);
                    }

                }
                //添加 符合气瓶
                String[] qpList = receiptTbBeanDTO.getQpList();
                for(int c =0;c<=qpList.length-1;c++){

                    if(receiptTbBeanDTO.getWtf().equals("辽河油田录井公司")){

                        String zhBsbh = qpList[c]+"LH";
                        WorkBean workBean = new WorkBean(receiptTbBeanDTO.getRwdbh(),2, zhBsbh,
                                receiptTbBeanDTO.getWtfId(), receiptTbBeanDTO.getWtf(), receiptTbBeanDTO.getSjr(), user.getUserId(),  user.getUserName(), receiptTbBeanDTO.getJsrq(), 1);
                        workBean.setAddUser(user.getUserId()+"");
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(receiptTbBeanDTO.getJsrq());
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddTime(TimeUtil.currentTimeMillis());
                        workBean.setWtfchId(receiptTbBeanDTO.getWtfchId());
                        workBean.setWtfch(receiptTbBeanDTO.getWtfch());

                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("YTQP","");
                        workBean.setPxzd(pxzdStr);

                        workMapper.addWork(workBean);
                    }else{
                        String zhBsbh = qpList[c];
                        WorkBean workBean = new WorkBean(receiptTbBeanDTO.getRwdbh(),2, zhBsbh,
                                receiptTbBeanDTO.getWtfId(), receiptTbBeanDTO.getWtf(), receiptTbBeanDTO.getSjr(), user.getUserId(),  user.getUserName(), receiptTbBeanDTO.getJsrq(), 1);
                        workBean.setAddUser(user.getUserId()+"");
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(receiptTbBeanDTO.getJsrq());
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddTime(TimeUtil.currentTimeMillis());
                        workBean.setWtfchId(receiptTbBeanDTO.getWtfchId());
                        workBean.setWtfch(receiptTbBeanDTO.getWtfch());
                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("YTQP","");
                        workBean.setPxzd(pxzdStr);
                        workMapper.addWork(workBean);
                    }
                }

                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("添加成功");


                //如果操作日志的开关已经打开
                if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                    //添加操作记录
                    // 先取得用户ip
                    String ip = IpUtil.getIpAddr(request);
                    //被操作者名字
                    String namesStr = JSON.toJSONString(receiptTbBeanDTO.getRwdbh());
                    //被操作者id
                    String bopIdStr = JSON.toJSONString(receiptTbBeanDTO.getRwdbh());
                    HistoryBean historyBean = new HistoryBean();
                    historyBean.setMessage("[" + user.getUserName() + "] 新增了任务单编号为 " + receiptTbBeanDTO.getRwdbh() + " 的样品接收单");
                    historyBean.setType(Opt.YPGL_YPJSD_XZJSD);
                    historyBean.setOpUserId(user.getUserId());
                    historyBean.setIp(ip);
                    historyBean.setBopId(bopIdStr);
                    historyBean.setAddTime(TimeUtil.currentTimeMillis());
                    historyBean.setAddUser(String.valueOf(user.getUserId()));
                    historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                    historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                    //插入数据库
                    historyMapper.addHistory(historyBean);
                }


            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 新增接收单
     * @param request
     * @param rwdbh
     * @param hxqNumber
     * @param fhqpNumber
     * @param wtfId
     * @param wtf
     * @param sjr
     * @param jsrq
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultMsg addReceipt2(HttpServletRequest request, String rwdbh, int hxqNumber, int fhqpNumber,
                                 long wtfId, String wtf, String sjr,String sjrPhone,
                                String jsrq,String bz,long wtfchId,String wtfch,
                                int mzjy , int mzwx,int qpjy,int qpcz,int ghqp,int ghpf,int isManual,String add_mz_st,String add_mz_ed,String add_qp_st,String add_qp_ed){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_YPJSD)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            if("".equals(hxqNumber)|| (hxqNumber==0 && fhqpNumber ==0) ){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }



            if("".equals(add_mz_st.trim()) &&  "".equals(add_qp_st.trim())){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("面罩开始号和气瓶开始号不能同时为空");
                return ret;
            }

            if(hxqNumber>0 ){
                if(add_mz_st.trim().length()<5){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("面罩开始号至少为5位");
                    return ret;
                }
            }

            if(fhqpNumber>0){
                if(add_qp_st.trim().length()<5){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("气瓶开始号至少为5位");
                    return ret;
                }
            }

            if(hxqNumber>1){
                if(add_mz_ed.trim().length()<5){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("面罩结束号至少为5位");
                    return ret;
                }
                if(add_mz_st.trim().length() != add_mz_ed.trim().length()){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("面罩开始号和结束号位数不一致");
                    return ret;

                }
            }

            if(fhqpNumber>1){
                if(add_qp_ed.trim().length()<5){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("气瓶结束号至少为5位");
                    return ret;
                }
                if(add_qp_st.trim().length() != add_qp_ed.trim().length()){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("气瓶开始号和结束号位数不一致");
                    return ret;
                }
            }




            //在做一次检查  看看有没有 重复的 标识编号在数据库
            //创建呼吸器数组
            String[] hxqArray= new String[hxqNumber];
            for(int j =0;j<hxqNumber;j++){
                int startNumberInt = Integer.parseInt(add_mz_st);
                int mybsbh =startNumberInt+j;
                String mybsbhStr =String.valueOf(mybsbh);
                int mlength = add_mz_st.length() -mybsbhStr.length();
                if(wtf.equals("辽河油田录井公司")){
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh)+"LH";;
                    hxqArray[j] = zhBsbh;
                }else{
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh);
                    hxqArray[j] = zhBsbh;
                }

            }
            //添加 符合气瓶
            //创建气瓶数组
            String[] qpArray= new String[fhqpNumber];
            for(int k =0;k<fhqpNumber;k++){
                int startNumberInt = Integer.parseInt(add_qp_st);
                int mybsbh =startNumberInt+k;
                String mybsbhStr =String.valueOf(mybsbh);
                int mlength = add_qp_st.length() -mybsbhStr.length();
                if(wtf.equals("辽河油田录井公司")){
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh) +"LH";
                    qpArray[k] = zhBsbh;
                }else{
                    String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh);
                    qpArray[k] = zhBsbh;
                }

            }
            List<WorkBean> workBeansALL =new ArrayList<>();

            if(hxqArray.length != 0){
                List<WorkBean> workBeans33 = workMapper.batchBsbhList(hxqArray);
                workBeansALL.addAll(workBeans33);
            }

            if(qpArray.length != 0){
                List<WorkBean> workBeans44 = workMapper.batchBsbhList(qpArray);
                workBeansALL.addAll(workBeans44);
            }

            List<String > basbhss = new ArrayList<>();
            if(workBeansALL.size()>0){
                String cfBsbh ="";
                for(WorkBean mw:workBeansALL){
                    String zybh= mw.getBsbh();
                    basbhss.add(zybh.substring(4,zybh.length()));
                }
                ret.setCode(88);
                ret.setData(basbhss);
                return ret;
            }

            //查询如果有一个一样的任务单就提示
            ReceiptTbBean receiptTbBean = receiptTbMapper.getReceiptByrwdbh(rwdbh);
            if(null != receiptTbBean){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("此任务单已经存在，不能重复添加");
                return ret;
            }

            //计算最大值和最小值
            String  startNumber;
            String endNumber;
            if(hxqNumber!=0 && fhqpNumber==0){  //只有面罩
                startNumber = add_mz_st;
                endNumber = add_mz_ed;
            }else if(hxqNumber ==0 && fhqpNumber !=0){ //只有气瓶
                startNumber = add_qp_st;
                endNumber = add_qp_ed;

            }else{  //两个都有
                int zmInt=0;
                int qpInt=0;
                if(!"".equals(add_mz_st)){
                    zmInt= Integer.parseInt(add_mz_st);
                }
                if(!"".equals(add_qp_st)){
                    qpInt = Integer.parseInt(add_qp_st);
                }
                if(zmInt>qpInt){
                    startNumber = qpInt+"";
                }else{
                    startNumber = zmInt+"";
                }

                int zmInt2=0;
                int qpInt2=0;
                if(!"".equals(add_mz_ed)){
                    zmInt2= Integer.parseInt(add_mz_ed);
                }
                if(!"".equals(add_qp_ed)){
                    qpInt2 = Integer.parseInt(add_qp_ed);
                }

                if(zmInt2 ==0  && qpInt2==0){
                    endNumber="";
                }else{
                    if(zmInt2>qpInt2){
                        endNumber = zmInt2+"";
                    }else{
                        endNumber = qpInt2+"";
                    }
                }
            }



            ReceiptTbBean receiptTbBean2 = new ReceiptTbBean(rwdbh, hxqNumber, fhqpNumber, startNumber, endNumber,
                    wtfId,  wtf, sjr, sjrPhone, user.getUserId(), user.getUserName(), jsrq,bz, 0);
            receiptTbBean2.setAddTime(TimeUtil.currentTimeMillis());
            receiptTbBean2.setAddUser(user.getUserId()+"");
            receiptTbBean2.setUpdateUser("");
            receiptTbBean2.setUpdateTime(0);
            receiptTbBean2.setWtfchId(wtfchId);
            receiptTbBean2.setWtfch(wtfch);

            receiptTbBean2.setMzjy(mzjy);
            receiptTbBean2.setMzwx(mzwx);
            receiptTbBean2.setQpjy(qpjy);
            receiptTbBean2.setQpcz(qpcz);
            receiptTbBean2.setGhqp(ghqp);
            receiptTbBean2.setGhpf(ghpf);
            int res =  receiptTbMapper.addReceipt(receiptTbBean2);



            //添加成功之后
            if(res>0){
                //添加呼吸器
                for(int a =0;a<hxqNumber;a++){
                    int startNumberInt = Integer.parseInt(add_mz_st);
                    int mybsbh =startNumberInt+a;
                    String mybsbhStr =String.valueOf(mybsbh);
                    int mlength = add_mz_st.length() -mybsbhStr.length();
                    if(wtf.equals("辽河油田录井公司")){
                        String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh)+"LH";
                        WorkBean workBean = new WorkBean(rwdbh,1, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddUser(user.getUserId()+"");
                        workBean.setAddTime(TimeUtil.currentTimeMillis());

                        workBean.setWtfchId(wtfchId);
                        workBean.setWtfch(wtfch);

                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("DQFJ","");
                        workBean.setPxzd(pxzdStr);
                        workMapper.addWork(workBean);
                    }else{
                        String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,1,rwdbh);
                        WorkBean workBean = new WorkBean(rwdbh,1, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                        workBean.setAddUser(user.getUserId()+"");
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddTime(TimeUtil.currentTimeMillis());
                        workBean.setWtfchId(wtfchId);
                        workBean.setWtfch(wtfch);

                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("DQFJ","");
                        workBean.setPxzd(pxzdStr);


                        workMapper.addWork(workBean);
                    }

                }
                //添加 符合气瓶
                for(int c =0;c<fhqpNumber;c++){
                    int startNumberInt = Integer.parseInt(add_qp_st);
                    int mybsbh =startNumberInt+c;
                    String mybsbhStr =String.valueOf(mybsbh);
                    int mlength = add_qp_st.length() -mybsbhStr.length();
                    if(wtf.equals("辽河油田录井公司")){
                        String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh)+"LH";;
                        WorkBean workBean = new WorkBean(rwdbh,2, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                        workBean.setAddUser(user.getUserId()+"");
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddTime(TimeUtil.currentTimeMillis());
                        workBean.setWtfchId(wtfchId);
                        workBean.setWtfch(wtfch);

                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("YTQP","");
                        workBean.setPxzd(pxzdStr);

                        workMapper.addWork(workBean);
                    }else{
                        String zhBsbh = NumberUtil.calculationBsbh2(mybsbh,mlength,2,rwdbh);
                        WorkBean workBean = new WorkBean(rwdbh,2, zhBsbh, wtfId, wtf, sjr, user.getUserId(),  user.getUserName(), jsrq, 1);
                        workBean.setAddUser(user.getUserId()+"");
                        //将接收日期转化为时间戳
                        long jsrqJjc = TimeUtil.formatDate_5(jsrq);
                        workBean.setJsrqLong(jsrqJjc);
                        workBean.setAddTime(TimeUtil.currentTimeMillis());
                        workBean.setWtfchId(wtfchId);
                        workBean.setWtfch(wtfch);
                        //添加排序字段
                        String pxzdStr = workBean.getBsbh().replace("YTQP","");
                        workBean.setPxzd(pxzdStr);
                        workMapper.addWork(workBean);
                    }
                }

                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("添加成功");


                //如果操作日志的开关已经打开
                if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                    //添加操作记录
                    // 先取得用户ip
                    String ip = IpUtil.getIpAddr(request);
                    //被操作者名字
                    String namesStr = JSON.toJSONString(rwdbh);
                    //被操作者id
                    String bopIdStr = JSON.toJSONString(rwdbh);
                    HistoryBean historyBean = new HistoryBean();
                    historyBean.setMessage("[" + user.getUserName() + "] 新增了任务单编号为 " + rwdbh + " 的样品接收单");
                    historyBean.setType(Opt.YPGL_YPJSD_XZJSD);
                    historyBean.setOpUserId(user.getUserId());
                    historyBean.setIp(ip);
                    historyBean.setBopId(bopIdStr);
                    historyBean.setAddTime(TimeUtil.currentTimeMillis());
                    historyBean.setAddUser(String.valueOf(user.getUserId()));
                    historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                    historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                    //插入数据库
                    historyMapper.addHistory(historyBean);
                }


            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 分页查询 样品接收单
     * @param request
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getReceiptListPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String rwdbh,long wtfId,long wtfchId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        List<ReceiptTbBean> receiptTbBeanList = new ArrayList<ReceiptTbBean>();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_YPYL)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }
            List<ReceiptTbBean>  data =  receiptTbMapper.getReceiptListPage(pageNum,pageSize,rwdbh,wtfId,wtfchId);
            long total = receiptTbMapper.getReceiptTotalNumber(rwdbh,wtfId,wtfchId);
            RespPageBean bean = new RespPageBean();
            bean.setData(data);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询最新的接受单
     * @param request
     * @return
     */
    public ResultMsg getReceiptFirst(HttpServletRequest request){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            ret = new ResultMsg(); // 声明一个返回值
            ReceiptTbBean receiptTbBean =  receiptTbMapper.getReceiptFirst();
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(receiptTbBean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;

    }



    /**
     * 分页查询 样品接收单
     * @param request
     * @param pageNum
     * @param pageSize
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getReceiptWqzListPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String rwdbh,long wtfId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        List<ReceiptTbBean> receiptTbBeanList = new ArrayList<ReceiptTbBean>();
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_YPYL)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }
            List<ReceiptTbBean>  data =  receiptTbMapper.getReceiptWqzListPage(pageNum,pageSize,rwdbh,wtfId);
            long total = receiptTbMapper.getReceiptWqzTotalNumber(rwdbh,wtfId);
            RespPageBean bean = new RespPageBean();
            bean.setData(data);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 根据任务单编号 删除接收单
     * @param request
     * @param rwdbh
     * @return
     */
    public ResultMsg delReceiptByrwdbh(HttpServletRequest request,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);

            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.YPGL_YPJSD)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            if(!CheckTool.checkIsAdmin(user)){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("你没有对应的操作权限");
                return ret;
            }

            //删除接收单
           long ress =  receiptTbMapper.delReceiptByrwdbh(rwdbh);
            if(ress>0){
                //删除工作单
                workMapper.deleteWorkByrwdbh(rwdbh);
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("删除成功");
            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置 驳回工作量确认单
     * @param request
     * @param jsdId
     * @param bhgyy
     * @return
     */
    public ResultMsg rejectQrdDal(HttpServletRequest request,long jsdId,String bhgyy,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DPZGZLQRD)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            long res = receiptTbMapper.updateReceiptTbBytp(jsdId,bhgyy);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                //如果操作日志的开关已经打开
                if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                    //添加操作记录
                    // 先取得用户ip
                    String ip = IpUtil.getIpAddr(request);
                    //被操作者id
                    String bopIdStr = JSON.toJSONString(jsdId);
                    HistoryBean historyBean = new HistoryBean();
                    historyBean.setMessage("[" + user.getUserName() + "] 驳回了任务单位 " + rwdbh + " 的 工作量确认单");
                    historyBean.setType(Opt.BGGL_BHGZLQRD);
                    historyBean.setOpUserId(user.getUserId());
                    historyBean.setIp(ip);
                    historyBean.setBopId(bopIdStr);
                    historyBean.setAddTime(TimeUtil.currentTimeMillis());
                    historyBean.setAddUser(String.valueOf(user.getUserId()));
                    historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                    historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                    //插入数据库
                    historyMapper.addHistory(historyBean);
                }
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候再试");
                return ret;
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置 工作量确认单转到下一级
     * @param request
     * @param jsdId
     * @return
     */
    public ResultMsg srtQrdProgress(HttpServletRequest request,long jsdId,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_GZLQRD)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            long res = receiptTbMapper.updateReceiptNext(jsdId);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候再试");
                return ret;
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置 过 通工作量确认单
     * @param request
     * @param jsdId
     * @return
     */
    public ResultMsg srtQrdOkProgress(HttpServletRequest request,long jsdId,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以新增客户信息
//            if (!CheckTool.checkAuthority(user, AuthorityID.BGGL_DPZGZLQRD)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            String qzrq = TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());

            long res = receiptTbMapper.srtQrdOkProgress(jsdId,user.getUserId(),user.getUserName(),qzrq);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常，请稍候再试");
                return ret;
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


}
